﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MSCISDevMetricHelper
{

    public class CommonHelper
    {

        public static void writelog(string message)
        {
            DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\Log");
            if (!di.Exists)
            {
                di.Create();
            }
            using (StreamWriter sw = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "\\Log", "Log" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt"), true))
            {
                sw.WriteLine(DateTime.Now.ToString("HH:mm:ss fff") + "   " + message);
                sw.Flush();
                sw.Close();
            }
        }

        public static string GetConfigValue(string ConfigName)
        {
            ConfigurationManager.RefreshSection("appSettings");
            return ConfigurationManager.AppSettings[ConfigName].ToString();
        }

        public static void UpdateAppConfig(string newKey, string newValue)
        {
            bool isModified = false;
            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key == newKey)
                {
                    isModified = true;
                }
            }       // Open App.Config of executable      
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);      // You need to remove the old settings object before you can replace it      
            if (isModified)
            {
                config.AppSettings.Settings.Remove(newKey);
            }          // Add an Application Setting.      
            config.AppSettings.Settings.Add(newKey, newValue);         // Save the changes in App.config file.      
            config.Save(ConfigurationSaveMode.Modified);      // Force a reload of a changed section.      
            ConfigurationManager.RefreshSection("appSettings");

        }
        public static string EncodeMD5(string strToEncode)
        {
            //Encode username
            MD5 md5Hash = MD5.Create();
            byte[] encodedata = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(strToEncode));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < encodedata.Length; i++)
            {
                sBuilder.Append(encodedata[i].ToString("x2"));
            }

            //Encode username end
            return sBuilder.ToString();

        }

        public static string ReadInfoXmlSingleNode(FileInfo fiXml, string NodePath)
        {
            System.Xml.XmlDocument form = new XmlDocument();
            form.Load(fiXml.FullName);
            XmlNode node = form.SelectSingleNode(NodePath, GetNameSpaceManager(form));
            return node.InnerText;
        }
        protected static XmlNamespaceManager GetNameSpaceManager(XmlDocument Document)
        {
            XmlNamespaceManager objXmlNamespaceManager = new XmlNamespaceManager(Document.NameTable);
            objXmlNamespaceManager.AddNamespace("z", Document.DocumentElement.GetNamespaceOfPrefix("z"));
            return objXmlNamespaceManager;
        }




    }

}

